package com.authine.cloudpivot.web.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.authine.cloudpivot.web.api.controller.base.BaseController;
import com.authine.cloudpivot.web.api.exception.PortalException;
import com.authine.cloudpivot.web.api.service.FescoadQueryService;
import com.authine.cloudpivot.web.api.service.StoreService;
import com.authine.cloudpivot.web.api.view.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.GET;
import java.util.Map;

@Api(description = "外企德科相关接口", tags = "门店信息")
@RestController
@RequestMapping("/api/store")
@Slf4j
@Validated
public class StoreController extends BaseController {

    @Autowired
    private StoreService storeService;

    /**
     * 获取门店编号，回写页面
     * @param
     * @return
     */
    @ApiOperation(value = "获取门店编号，回写页面" )
    @GetMapping("/getStoreNum")
    @ResponseBody
    public ResponseResult<?> getStoreNum(String storeNum){
        JSONObject json = new JSONObject();
        Long statusReal = 200L;
        try {
            String existNum;
            if(StringUtils.isEmpty(storeNum)){
                String staticStr = "ST";
                existNum= storeService.getStoreNum();
                if (StringUtils.isEmpty(existNum)) {
                    existNum = "ST000001";
                } else {
                    existNum= storeService.createStoreNum(staticStr, existNum);
                }
            }else{
                existNum=storeNum;
            }
            json.put("storeNum",existNum);
            json.put("message","生成门店编号成功");
        }catch (PortalException e) {
                statusReal = 500L;
                json.put("message", "生成门店编号失败：" + e.getErrMsg());
        }
        return ResponseResult.<JSONObject>builder().errcode(statusReal).data(json).build();
    }

    /**
     * 更新门店编号
     * @param objectId
     * @param storeNum
     * @param
     */
    @ApiOperation(value = "更新门店编号" )
    @GetMapping("/updateStoreNum")
    @ResponseBody
    public ResponseResult<?> updateStoreNum(String objectId,String storeNum){
        JSONObject json = new JSONObject();
        Long statusReal = 200L;
        try{
            storeService.updateStoreNum(objectId,storeNum);
            json.put("message", "修改门店编号成功");
        }catch (PortalException e) {
            statusReal = 500L;
            json.put("message", "修改门店编号失败：" + e.getErrMsg());
        }
        return ResponseResult.<JSONObject>builder().errcode(statusReal).data(json).build();
    }


    /**
     * @Description : 获取城市等级
     * @param ： province
     * @param ： cityName
     * @author : Liun
     * @date : 2020/4/10
     */
    @ApiOperation(value = "获取城市等级" )
    @GetMapping("/getCityLevel")
    @ResponseBody
    public ResponseResult<?> getCityLevel(String province,String cityName){
        JSONObject json = new JSONObject();
        Long statusReal = 200L;
        try {
            String cityLevel = storeService.getCityLevel(province,cityName);
            if(StringUtils.isEmpty(cityLevel)){
                cityLevel="当前城市无等级，请维护！";
            }
                json.put("cityLevel",cityLevel);
                json.put("message","获取城市等级成功");
        }catch (PortalException e) {
            statusReal = 500L;
            json.put("message", "获取城市等级失败：" + e.getErrMsg());
        }
        return ResponseResult.<JSONObject>builder().errcode(statusReal).data(json).build();
    }


    /**
     * @Description : 根据项目和门店信息，获取当前门店督导
     * @param ： projectName
     * @param ： storeName
     * @author : Liun
     * @date : 2020/4/22
     */
    @ApiOperation(value = "获取督导" )
    @GetMapping("/getMember")
    @ResponseBody
    public ResponseResult<?> getMember(String projectName,String storeName){
        JSONObject json = new JSONObject();
        Long statusReal = 200L;
        try {
            String member = storeService.getMember(projectName,storeName);
            JSONArray person =new JSONArray();
            if(StringUtils.isNotEmpty(member)){
                 person = JSONArray.fromObject(member);
            }
            json.put("member",person);
            json.put("message","获取督导成功");
        }catch (PortalException e) {
            statusReal = 500L;
            json.put("message", "获取督导失败：" + e.getErrMsg());
        }
        return ResponseResult.<JSONObject>builder().errcode(statusReal).data(json).build();
    }


}
